/*
 * @Author: ZephyrGao
 * @Date: 2024-02-27 08:01:27
 * @Description: 
 * Copyright (c) 2024 by ZephyrGao, All Rights Reserved. 
 */
// test.ts

import { MockMethod, MockConfig } from 'vite-plugin-mock'

const database = {
  userList: [
    {
      id: 1,
      name: 'vben',
      email: 'vben@vben',
      password: '123456',
      roles: ['admin'],
    },
    {
      id: 2,
      name: 'gai',
      email: '312@vben',
      password: '12345622',}
  ],
}

export default [
  {
    url: '/api/getName',
    method: 'get',
    response: ({ }) => {
      return {
        code: 0,
        data: {
          name: 'getName',
        },
      }
    },
  },
  {
    url: '/api/getUser',
    method: 'post',
    timeout: 2000,
    response: {
      code: 0,
      data: {
        name: 'getUser',
      },
    },
  },
  {
    url: '/api/text',
    method: 'post',
    rawResponse: async (req, res) => {
      debugger
      let reqbody = ''
      await new Promise((resolve) => {
        req.on('data', (chunk) => {
          
          reqbody += chunk
        })
        req.on('end', () => resolve(undefined))
      })
      res.setHeader('Content-Type', 'text/plain')
      res.statusCode = 200
      res.end(`hello, ${reqbody}`)
    },
  },
] as MockMethod[]


// export default function (config: MockConfig) {
//   return [
//     {
//       url: '/api/text',
//       method: 'post',
//       rawResponse: async (req:any, res:any) => {
//         let reqbody = ''
//         await new Promise((resolve) => {
//           req.on('data', (chunk:any) => {
//             reqbody += chunk
//           })
//           req.on('end', () => resolve(undefined))
//         })
//         res.setHeader('Content-Type', 'text/plain')
//         res.statusCode = 200
//         res.end(`hello, ${reqbody}`)
//       },
//     },
//   ]
// }